package com.java.main.domain.IdEntity;

import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import java.util.Date;

/**
 * 继承IdEntityAuto的对象，在创建或更新前自动加入时间戳
 * 
 * @author sunyang
 *
 */
public class IdEntityAssignedListener {

	@PrePersist
	public void prePersist(IdEntityAssigned idEntityAssigned) {
		Date now = new Date();
		idEntityAssigned.setCreateTime(now);
		idEntityAssigned.setModifyTime(now);
//		KbmContent kbmContent = idEntityAssigned.getKbmContent();
//		if (kbmContent == null) {
//			throw new HiBabyGlobalException(Errors.LACK_CONTENT);
//		}
//		if (kbmContent.getCurrentUserId() == null) {
//			throw new HiBabyGlobalException(Errors.NOT_LOGIN);
//		}
//		if (kbmContent.getEndemicId() == null) {
//			throw new HiBabyGlobalException(Errors.NO_ENDEMIC);
//		}
//		idEntityAssigned.setCreateId(kbmContent.getCurrentUserId());
//		idEntityAssigned.setModifiyId(kbmContent.getCurrentUserId());
//		idEntityAssigned.setEndemicId(kbmContent.getEndemicId());
		idEntityAssigned.setSoftDelete(0);
	}

	@PreUpdate
	public void preUpdate(IdEntityAssigned idEntityAssigned) {
		Date now = new Date();
		idEntityAssigned.setModifyTime(now);
//		KbmContent kbmContent = idEntityAssigned.getKbmContent();
//		if (kbmContent == null) {
//			throw new HiBabyGlobalException(Errors.LACK_CONTENT);
//		}
//		idEntityAssigned.setModifiyId(kbmContent.getCurrentUserId());

	}

	@PreRemove
	public void preRemove(IdEntityAssigned idEntityAssigned) {
//		KbmContent kbmContent = idEntityAssigned.getKbmContent();
//		if (kbmContent == null) {
//			throw new HiBabyGlobalException(Errors.LACK_CONTENT);
//		}
//		if (!kbmContent.getDel().equals(1)) {
//			throw new HiBabyGlobalException(Errors.SYSTEM_ERROR);
//		}

	}
}
